﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.Data;
using System.Collections.Generic;

using AJRavindiran.Jolt.Storage;
using AJRavindiran.Jolt.RuneScape.Models.Characters;

namespace AJRavindiran.Jolt.RuneScape.Database
{
    /// <summary>
    /// Represents management for offenses.
    /// </summary>
    public class OffenseManager
    {
        #region Fields
        #endregion Fields

        #region Methods
        /// <summary>
        /// Gets the type of offense the given user has.
        /// </summary>
        /// <param name="username">The username to check.</param>
        /// <returns>Returns 
        /// <code>-1</code> if no offense.
        /// <code>0</code> if muted offense.
        /// <code>1</code> if ban offense.
        /// <code>2</code> if ip ban offense.</returns>
        public int GetOffense(string username)
        {
            DataRow dataRow;
            using (DatabaseClient dbClient = Jolt.GetDatabase().GetClient())
            {
                dbClient.AddParamWithValue("username", username);
                dataRow = dbClient.ReadDataRow("SELECT username, ban_type, ban_expire_date FROM banned WHERE username = @username AND expired = '0' LIMIT 1;");
            }

            try
            {
                if (dataRow != null && dataRow.Table.Rows.Count > 0)
                {
                    if (DateTime.Now >= (DateTime)dataRow[2])
                    {
                        this.RemoveOffense(username);
                        return -1;
                    }
                    return (int)dataRow[1];
                }
            }
            catch (Exception ex)
            {
                Jolt.GetLog().WriteException(ex);
            }
            return -1;
        }

        /// <summary>
        /// Gets the type of offense the given user has.
        /// </summary>
        /// <param name="username">The character instance.</param>
        /// <returns>Returns 
        /// <code>-1</code> if no offense.
        /// <code>0</code> if muted offense.
        /// <code>1</code> if ban offense.
        /// <code>2</code> if ip ban offense.</returns>
        public int GetOffense(Character character)
        {
            return this.GetOffense(character.GetDetails().Username);
        }

        /// <summary>
        /// Gets the type of offense the given user has.
        /// </summary>
        /// <param name="username">The username to check.</param>
        /// <returns>Returns 
        /// <code>OffenseTypes.None</code> if no offense.
        /// <code>OffenseTypes.Mute</code> if muted offense.
        /// <code>OffenseTypes.Ban</code> if ban offense.
        /// <code>OffenseTypes.IPBan</code> if ip ban offense.</returns>
        public OffenseTypes GetOffenseType(string username)
        {
            DataRow dataRow;
            using (DatabaseClient dbClient = Jolt.GetDatabase().GetClient())
            {
                dbClient.AddParamWithValue("username", username);
                dataRow = dbClient.ReadDataRow("SELECT username, ban_type, ban_expire_date FROM banned WHERE username = @username AND expired = '0' LIMIT 1;");
            }

            try
            {
                if (dataRow != null && dataRow.Table.Rows.Count > 0)
                {
                    if (DateTime.Now >= (DateTime)dataRow[2])
                    {
                        this.RemoveOffense(username);
                        return OffenseTypes.None;
                    }
                    return (OffenseTypes)Convert.ToInt32(dataRow[1]);
                }
            }
            catch (Exception ex)
            {
                Jolt.GetLog().WriteException(ex);
            }
            return OffenseTypes.None;
        }

        /// <summary>
        /// Gets the type of offense the given user has.
        /// </summary>
        /// <param name="character">The character instance.</param>
        /// <returns>Returns 
        /// <code>OffenseTypes.None</code> if no offense.
        /// <code>OffenseTypes.Mute</code> if muted offense.
        /// <code>OffenseTypes.Ban</code> if ban offense.
        /// <code>OffenseTypes.IPBan</code> if ip ban offense.</returns>
        public OffenseTypes GetOffenseType(Character character)
        {
            return this.GetOffenseType(character.GetDetails().Username);
        }

        /// <summary>
        /// Removes an offense from the database.
        /// </summary>
        /// <param name="username">Character's username.</param>
        public void RemoveOffense(string username)
        {
            using (DatabaseClient dbClient = Jolt.GetDatabase().GetClient())
            {
                dbClient.AddParamWithValue("username", username);
                dbClient.ExecuteQuery("UPDATE offenses SET expired = '1' WHERE username = @username AND expired = '0'");
            }
        }
        #endregion Methods
    }
}
